//
//  GoodsBottomView.m
//  Mall
//
//  Created by 孙飞燕 on 16/10/11.
//  Copyright © 2016年 孙飞燕. All rights reserved.
//

#import "GoodsBottomView.h"
#import "LoginViewController.h"
@implementation GoodsBottomView

- (instancetype)initWithFrame:(CGRect)frame inView:(UIView *)parentView{
    self.parentView = parentView;
    self =  [super initWithFrame:frame];
    if (self) {

        //联系客服按钮设置
        _serviceBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_serviceBtn addTarget:self action:@selector(serviceAction:) forControlEvents:UIControlEventTouchUpInside];
        _serviceBtn.frame = CGRectMake(0, 0,CGRectGetWidth(self.frame)/4, CGRectGetHeight(self.frame));
        [_serviceBtn setTitle:@"联系客服" forState:UIControlStateNormal];
        _serviceBtn.backgroundColor = [UIColor whiteColor];
        _serviceBtn.tintColor = [UIColor blackColor];

        //收藏按钮
        _collectionBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_collectionBtn addTarget:self action:@selector(collectionAction:) forControlEvents:UIControlEventTouchUpInside];
        _collectionBtn.frame = CGRectMake(CGRectGetWidth(self.frame)/4, 0, CGRectGetWidth(self.frame)/4, CGRectGetHeight(self.frame));
        [_collectionBtn setTitle:@"收藏" forState:UIControlStateNormal];
        _collectionBtn.backgroundColor = [[UIColor yellowColor]colorWithAlphaComponent:0.6];
        _collectionBtn.tintColor = [UIColor whiteColor];


        //加入购物车按钮设置
        _shopCarBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_shopCarBtn addTarget:self action:@selector(shopCarAction:) forControlEvents:UIControlEventTouchUpInside];
        _shopCarBtn.frame = CGRectMake(CGRectGetWidth(self.frame)/4*2, 0,CGRectGetWidth(self.frame)/4, CGRectGetHeight(self.frame));
        [_shopCarBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        _shopCarBtn.backgroundColor = [UIColor magentaColor];
        _shopCarBtn.tintColor = [UIColor whiteColor];

        //立即购买按钮设置
        _buyBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_buyBtn addTarget:self action:@selector(buyAction:) forControlEvents:UIControlEventTouchUpInside];
        _buyBtn.frame = CGRectMake(CGRectGetWidth(self.frame)/4*3, 0,CGRectGetWidth(self.frame)/4, CGRectGetHeight(self.frame));
        [_buyBtn setTitle:@"立即购买" forState:UIControlStateNormal];
        _buyBtn.backgroundColor = [UIColor redColor];
        _buyBtn.tintColor = [UIColor whiteColor];
        
        //将按钮添加到view上
        [self addSubview:_serviceBtn];
        [self addSubview:_collectionBtn];
        [self addSubview:_shopCarBtn];
        [self addSubview:_buyBtn];

        //设置确定按钮
        _sureBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureAction:) forControlEvents:UIControlEventTouchUpInside];
        _sureBtn.frame = CGRectMake(CGRectGetWidth(self.frame)/2 - 25, 0, 50, CGRectGetHeight(self.frame));
        _sureBtn.backgroundColor = [UIColor orangeColor];





       //设置添加购物车的界面
        self.click = YES;
        self.goodsView = [[AddGoodsView alloc]initWithFrame:CGRectMake(0, CGRectGetHeight(self.parentView.frame)/2, CGRectGetWidth(self.frame), CGRectGetHeight(self.parentView.frame)/2)];


    }
    return self;
}

//按钮事件的回调方法
- (void)sureAction:(UIButton *)sureBtn{
    self.goodsView.hidden = YES;
    self.serviceBtn.hidden = NO;
    self.shopCarBtn.hidden = NO;
    self.buyBtn.hidden = NO;
    self.sureBtn.hidden = YES;
}

//联系客服事件
- (void)serviceAction:(UIButton *)serviceBtn{

}

//收藏事件
- (void)collectionAction:(UIButton *)sender{

}

//加入购物车事件
- (void)shopCarAction:(UIButton *)shopCarBtn{
    if (self.click == YES) {
        [UIView animateWithDuration:0.5f animations:^{

            //将AddGoodsView添加到GoodsBottomView的父视图上
            [self.parentView addSubview:self.goodsView];
            self.goodsView.frame = CGRectMake(0, CGRectGetHeight(self.parentView.frame)*0.5 - 50, CGRectGetWidth(self.frame), CGRectGetHeight(self.parentView.frame)*0.5 - 50);
            //添加确定按钮
            [self addSubview:_sureBtn];
            //隐藏其他三个按钮
            self.serviceBtn.hidden = YES;
            self.shopCarBtn.hidden = YES;
            self.buyBtn.hidden = YES;

        } completion:^(BOOL finished) {
            self.click = NO;
        }];
    }
    if (self.click == NO) {
        [UIView animateWithDuration:0.3f animations:^{
            self.goodsView.frame = CGRectMake(0, CGRectGetHeight(self.parentView.frame), CGRectGetWidth(self.frame), CGRectGetHeight(self.parentView.frame)*0.5);
        } completion:^(BOOL finished) {
            self.click = YES;
        }];
    }


    }

//立即购买
- (void)buyAction:(UIButton *)buyBtn{
    LoginViewController *loginVC = [[LoginViewController alloc]init];

}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
